package com.wmlive.hhvideo.jpush;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wmlive.hhvideo.application.AppStatusManager;
import com.wmlive.hhvideo.heihei.mainhome.activity.MainNewActivity;
import com.wmlive.hhvideo.heihei.splash.SplashActivity;
import com.wmlive.hhvideo.utils.KLog;
import com.wmlive.hhvideo.utils.preferences.SPUtils;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by XueFei on 2017/7/11.
 * <p>
 * 极光推送
 * <p>
 * 接收消息
 */

public class JpushReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            Bundle bundle = intent.getExtras();
            KLog.i("JpushReceiver", "[MyReceiver] onReceive - " + intent.getAction());
            if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
                String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
                KLog.i("JpushReceiver", "[MyReceiver] 接收Registration Id 第111遍第1111遍第111遍: " + regId);
                KLog.i("JpushReceiver", "[MyReceiver] 接收Registration Id 第二遍第二遍第二遍: " + regId);
            } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
                //接收自定义消息并notifacation
            } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
                //接收普通消息(通知)
            } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
                //用户点击打开了普通消息(通知)
                openNotification(context, bundle);
            } else if (JPushInterface.ACTION_NOTIFICATION_CLICK_ACTION.equals(intent.getAction())) {
                KLog.i("JpushReceiver", "[MyReceiver] ACTION_NOTIFICATION_CLICK_ACTION ACTION_NOTIFICATION_CLICK_ACTION ");
            } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {

            } else {

            }
        } catch (Exception e) {

        }
    }

    /**
     * 打开普通消息
     */
    private final String LINK = "link";

    private void openNotification(Context context, Bundle bundle) {
        String data = bundle.getString(JPushInterface.EXTRA_EXTRA);
        //获取link数据 例如:{"link":"hhvideo://topic/detail?id=10001"}
        JSONObject jsonObject = JSON.parseObject(data);
        if (null != jsonObject) {
            String link = jsonObject.getString(LINK);
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            if (AppStatusManager.getInstance().getAppStatus() == AppStatusManager.STATUS_FORCE_KILLED) {
                //未打开app,通过SplashActivity传递内容到首页
                intent.setClass(context, SplashActivity.class);
            } else {
                intent.setClass(context, MainNewActivity.class);
            }
            intent.putExtra(MainNewActivity.KEY_CONTENT, link);
            context.startActivity(intent);
        }
    }
}
